package com.dt.zbcy.provider.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dt.zbcy.provider.core.handle.DubboResult;
import com.dt.zbcy.provider.core.handle.PageResult;
import com.dt.zbcy.provider.core.handle.ReqPageQuery;
import com.dt.zbcy.provider.web.BaseController;
import com.dt.zbcy.provider.web.dto.ReqMjzDto;
import com.dt.zbcy.provider.web.entity.MjzUserInfoEntity;
import com.dt.zbcy.provider.web.service.IMjzUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: liuwenxiang
 * @Date: 2019/3/31 00:56
 * @Description:
 */
@Api(description = "媒介主")
@RestController
@RequestMapping("${url.common.path}/mjz")
public class MjzUserInfoController extends BaseController {

    @Autowired
    private IMjzUserInfoService mjzUserInfoService;




    @ApiOperation(value="获取媒介主用户列表",notes="")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public DubboResult list(@Valid ReqMjzDto req) {

        req.setSort(StringUtils.isNotBlank(req.getSort()) ? req.getSort() : ReqPageQuery.DB_MTIME);
        req.setOrder(StringUtils.isNotBlank(req.getOrder()) ? req.getOrder() : ReqPageQuery.DESC);

        long amount = mjzUserInfoService.selectAmount(req);
        List<MjzUserInfoEntity> mjzUserInfoList = new ArrayList<>();
        if(amount>0){
            mjzUserInfoList = mjzUserInfoService.selectListByPage(req);
        }
        PageResult<MjzUserInfoEntity> flowPage = new PageResult<MjzUserInfoEntity>(amount, req.getCurrentPage(), req.getPageSize(), mjzUserInfoList);
        return DubboResult.buildSuccessResult(flowPage);
    }


    @ApiOperation(value = "新增用户",notes = "")
    @ApiImplicitParam(name="mjzUser",value = "用户详细实体user",required = true,dataType ="mjzUserInfoEntity")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public DubboResult<String> save(@Valid @RequestBody ReqMjzDto req){
        String nickName =req.getNickName();
        MjzUserInfoEntity user = new MjzUserInfoEntity();
        user.setNickName(req.getNickName());
        user.setPassword(req.getPassword());
        user.setCUserId("1");
        user.setMUserId("1");
        mjzUserInfoService.insert(user);
        return DubboResult.buildSuccessResult(user.getCUserId());
    }

    @ApiOperation(value = "修改用户",notes = "")
    @ApiImplicitParam(name="mjzUser",value = "用户详细实体user",required = true,dataType ="mjzUserInfoEntity")
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public DubboResult edit(@Valid @RequestBody ReqMjzDto req){
        int userId = req.getId();
        String nickName =req.getNickName();
        MjzUserInfoEntity user = new MjzUserInfoEntity();
        user.setNickName(nickName);
        user.setTel(req.getTel());
        mjzUserInfoService.update(user,new EntityWrapper<MjzUserInfoEntity>().eq("id",userId));
        return DubboResult.buildSuccessResult();

    }


    @ApiOperation(value = "删除用户",notes = "")
    @ApiImplicitParam(name="id",value = "用户id",required = true,dataType ="Long")
    @RequestMapping(value = "del",method = RequestMethod.POST)
    public DubboResult delete(@Valid @RequestBody ReqMjzDto req){
        int id = req.getId();
        mjzUserInfoService.deleteById(id);
        return DubboResult.buildSuccessResult();

    }






}
